#!/exlibris/sfx_ver/sfx_version_3/app/perl/bin/perl

#######################################################################
#
#	CAL STATE CHAMELEON PLUGIN (CGI SCRIPT)
#	version 3.4
#
#	(c) 2009 California State University
#	By David Walker
#	dwalker@calstate.edu
#	
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#	Lesser General Public License for more details.
#
#	http://www.gnu.org/licenses/lgpl.txt
#
########################################################################

use Parsers::PlugIn::Chameleon_Test;
use Parsers::PlugIn::Chameleon_Local;
use CGI;

my $cgi = new CGI;

my $vendorInnopac = undef;
my $vendorEndeavor = undef;

my $issn = $cgi->param("issn");
my $isbn = $cgi->param("isbn");
my $year = $cgi->param("year");

my $vendor = $cgi->param("vendor");
my $address = $cgi->param("address");
my $target = $cgi->param("target");
my $port = $cgi->param("port");
my $database = $cgi->param("database");
my $recordSyntax = $cgi->param("syntax");
my $availability = $cgi->param("availability");
my $delimiter = $cgi->param("delimiter");
my $delimiter_replace = $cgi->param("delimiter_replace");
my $span = $cgi->param("span");
my $rolling_holdings = $cgi->param("rolling_holdings");
my $bib_fields = $cgi->param("bib_fields");

my $ignore_locations = $cgi->param("ignore_locations");
my $full_date_to_year = $cgi->param("full_date_to_year");
my $print_only = $cgi->param("print_only");

my $simplify_checked = "";

if ( $full_date_to_year ne undef )
{
	$simplify_checked = "checked";
}

if ( $print_only ne undef )
{
	$print_only_checked = "checked";
}

if ( $recordSyntax eq undef )
{
	$recordSyntax = "OPAC";
}

if ( $target ne undef )
{
	$address = $target;
}

if ( $vendor eq "Innovative" )
{
	$vendorInnopac = "checked";
}
elsif ( $vendor eq "Endeavor" )
{
	$vendorEndeavor = "checked";
}

# create mock context object

my $objContextObject = Parsers::PlugIn::Chameleon_Test->new();

$objContextObject->set('rft.issn', $issn );
$objContextObject->set('rft.isbn', $isbn );
$objContextObject->set('rft.year', $year );

# Chameleon Config

my $objChameleon = Parsers::PlugIn::Chameleon_Local->new();

# override ChameleonLocal with these parameters

$objChameleon->{'vendor'} = $vendor;
$objChameleon->{'address'} = $address;
$objChameleon->{'port'} = $port; 
$objChameleon->{'record_syntax'} = $recordSyntax; 
$objChameleon->{'database'} = $database;
$objChameleon->{'availability'} = $availability;
$objChameleon->{'rolling_holdings'} = $rolling_holdings;
$objChameleon->{'bib_fields'} = $bib_fields;
$objChameleon->{'ignore_locations'} = $ignore_locations;
$objChameleon->{'full_date_to_year'} = $full_date_to_year;
$objChameleon->{'print-only'} = $print_only;

if ( $delimiter ne "" )
{
	$objChameleon->{'delimiter'} = $delimiter;
}
if ( $delimiter_replace ne "" )
{
	$objChameleon->{'delimiter_replace'} = $delimiter_replace;
}
if ( $span ne "" )
{
	$objChameleon->{'span'} = $span;
}

# run it if there are values

if ( $vendor ne "" && $address ne "" && $port ne "" && $database ne "" )
{
	$objChameleon->lookup($objContextObject);
}

# print the results

print $cgi->header, $cgi->start_html('Chameleon Test');
print $cgi->h2('Chameleon Test');

print <<PAGE;

<script language="JavaScript" type="text/JavaScript">
function fillVendor()
{
	if ( document.form1.vendor[0].checked == true )
	{
		document.form1.port.value = "210";
		document.form1.database.value = "innopac";
	}
	else if ( document.form1.vendor[1].checked == true )
	{
		document.form1.port.value = "7090";
		document.form1.database.value = "voyager";	
	}
}

//Rich HTML Balloon Tooltip: http://www.dynamicdrive.com/dynamicindex5/balloontooltip.htm
//Created: September 10th, 2006

var disappeardelay=250  //tooltip disappear delay (in miliseconds)
var verticaloffset=0 //vertical offset of tooltip from anchor link, if any
var enablearrowhead=0 //0 or 1, to disable or enable the arrow image
var arrowheadimg=["arrowdown.gif", "arrowup.gif"] //path to down and up arrow images
var arrowheadheight=11 //height of arrow image (amount to reveal)

/////No further editting needed

var ie=document.all
var ns6=document.getElementById&&!document.all
verticaloffset=(enablearrowhead)? verticaloffset+arrowheadheight : verticaloffset

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function showhide(obj, e){
dropmenuobj.style.left=dropmenuobj.style.top="-500px"
if (e.type=="mouseover")
obj.visibility="visible"
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
if (whichedge=="rightedge"){
edgeoffsetx=0
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffsetx=dropmenuobj.contentmeasure-obj.offsetWidth
return edgeoffsetx
}
else{
edgeoffsety=0
var topedge=ie && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) //move up?
edgeoffsety=dropmenuobj.contentmeasure+obj.offsetHeight+(verticaloffset*2)
return edgeoffsety
}
}

function displayballoontip(obj, e){ //main ballooon tooltip function
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
if (typeof dropmenuobj!="undefined") //hide previous tooltip?
dropmenuobj.style.visibility="hidden"
clearhidemenu()
//obj.onmouseout=delayhidemenu
dropmenuobj=document.getElementById(obj.getAttribute("rel"))
showhide(dropmenuobj.style, e)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")+verticaloffset
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
if (enablearrowhead)
displaytiparrow()
}

function displaytiparrow(){ //function to display optional arrow image associated with tooltip
tiparrow=document.getElementById("arrowhead")
tiparrow.src=(edgeoffsety!=0)? arrowheadimg[0] : arrowheadimg[1]
var ieshadowwidth=(dropmenuobj.filters && dropmenuobj.filters[0])? dropmenuobj.filters[0].Strength-1 : 0
//modify "left" value depending on whether there's no room on right edge of browser to display it, respectively
tiparrow.style.left=(edgeoffsetx!=0)? parseInt(dropmenuobj.style.left)+dropmenuobj.offsetWidth-tiparrow.offsetWidth-10+"px" : parseInt(dropmenuobj.style.left)+5+"px"
//modify "top" value depending on whether there's no room on right edge of browser to display it, respectively
tiparrow.style.top=(edgeoffsety!=0)? parseInt(dropmenuobj.style.top)+dropmenuobj.offsetHeight-tiparrow.offsetHeight-ieshadowwidth+arrowheadheight+"px" : parseInt(dropmenuobj.style.top)-arrowheadheight+"px"
tiparrow.style.visibility="visible"
}

function delayhidemenu(){
delayhide=setTimeout("dropmenuobj.style.visibility='hidden'; dropmenuobj.style.left=0; if (enablearrowhead) tiparrow.style.visibility='hidden'",disappeardelay)
}

function clearhidemenu(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}

function reltoelement(linkobj){ //tests if a link has "rel" defined and it's the ID of an element on page
var relvalue=linkobj.getAttribute("rel")
return (relvalue!=null && relvalue!="" && document.getElementById(relvalue)!=null && document.getElementById(relvalue).className=="balloonstyle")? true : false
}

function initalizetooltip(){
var all_links=document.getElementsByTagName("a")
if (enablearrowhead){
tiparrow=document.createElement("img")
tiparrow.setAttribute("src", arrowheadimg[0])
tiparrow.setAttribute("id", "arrowhead")
document.body.appendChild(tiparrow)
}
for (var i=0; i<all_links.length; i++){
if (reltoelement(all_links[i])){ //if link has "rel" defined and it's the ID of an element on page
all_links[i].onmouseover=function(e){
var evtobj=window.event? window.event : e
displayballoontip(this, evtobj)
}
all_links[i].onmouseout=delayhidemenu
}
}
}

if (window.addEventListener)
window.addEventListener("load", initalizetooltip, false)
else if (window.attachEvent)
window.attachEvent("onload", initalizetooltip)
else if (document.getElementById)
window.onload=initalizetooltip

</script>
<style type="text/css">

a { 
	text-decoration: none;
	border-bottom: 1px dashed #666;
	color: #000;
}

.balloonstyle{
	position:absolute;
	top: -500px;
	left: 0;
	padding: 10px;
	visibility: hidden;
	border:1px solid black;
	font:normal 12px Verdana;
	line-height: 18px;
	z-index: 100;
	background-color: #FFFFCC;
	width: 450px;
}

#arrowhead{
z-index: 99;
position:absolute;
top: -500px;
left: 0;
visibility: hidden;
}

</style>


<form name="form1" action="chameleon.cgi">

<div>
	<div style='float:left; margin-right: 30px;'>
	<h3>Server Info</h3>
	<table border="0" cellspacing="0" cellpadding="5">
		<tr valign="top">
			<td>System:</td>
			<td><input name="vendor" type="radio" value="Innovative" onClick="fillVendor()" $vendorInnopac>
			  Innovative<br>
			  <input name="vendor" type="radio" value="Endeavor" onClick="fillVendor()" $vendorEndeavor>
			  Voyager</td>
		</tr>
		<tr valign="top">
			<td>address:</td>
			<td><input name="address" type="text" id="address" value="$address"></td>
		</tr>
		<tr valign="top">
			<td>port:</td>
			<td><input name="port" type="text" id="port" size="4" value="$port"></td>
		</tr>
		<tr valign="top">
			<td>database:</td>
			<td><input name="database" type="text" id="database" value="$database"></td>
		</tr>
		<tr valign="top">
			<td>record format:</td>
			<td><input name="syntax" type="text" id="syntax" value="$recordSyntax"></td>
		</tr>
	</table>
</div>
<div>
	<h3>Book Search</h3>
	<table border="0" cellspacing="0" cellpadding="5">
		<tr>
			<td>isbn:</td>
			<td><input name="isbn" type="text" id="isbn" value="$isbn"></td>
		</tr>
		<tr>
			<td> and <a href="#" rel="avail">availability messages</a> :</td>
			<td><textarea name="availability" cols="40" rows="2">$availability</textarea></td>
		</tr>
	</table>
	<h3>Journal Search</h3>
	<table border="0" cellspacing="0" cellpadding="5">
		<tr>
			<td>issn:</td>
			<td><input name="issn" type="text" id="issn" value="$issn"></td>
		</tr>
		<tr valign="top">
			<td> and year:</td>
			<td style="padding-bottom: 20px;"><input name="year" type="text" id="year" size="5" value="$year"></td>
		</tr>
		<tr>
			<td><a href="#" rel="ignore">ignore locations</a>:</td>
			<td><input name="ignore_locations" type="text" id="ignore_locations" value="$ignore_locations"></td>
		</tr>
		<tr>
			<td> <a href="#" rel="dates">simplify dates</a>:</td>
			<td><input name="full_date_to_year" type="checkbox" id="full_date_to_year" $simplify_checked></td>
		</tr>
		<tr>
			<td><a href="#" rel="runs">delimiter</a> :</td>
			<td><input name="delimiter" type="text" id="delimiter" size="2" value="$delimiter"></td>
		</tr>
		<tr>
			<td><a href="#" rel="runs">delimiter replace</a> :</td>
			<td><input name="delimiter_replace" type="text" id="delimiter_replace" size="2" value="$delimiter_replace"></td>
		</tr>
		<tr>
			<td><a href="#" rel="runs">span</a> :</td>
			<td><input name="span" type="text" id="span" size="2" value="$span"></td>
		</tr>
		<tr>
			<td><a href="#" rel="bib">bibliographic fields</a>:</td>
			<td><input name="bib_fields" type="text" id="bib_fields" value="$bib_fields"></td>
		</tr>
		<tr>
			<td><a href="#" rel="rolling">rolling holding statements</a> :</td>
			<td><textarea name="rolling_holdings" id="rolling_holdings" cols="40" rows="2">$rolling_holdings</textarea></td>
		</tr>
		<tr>
			<td><a href="#" rel="print-only">show for any print holdings</a> :</td>
			<td><input name="print_only" id="print_only" type="checkbox" $print_only_checked>$print_only</input></td>
		</tr>
		<tr>
			<td><input type="submit" name="Submit" value="Submit"></td>
			<td>&nbsp;</td>
		</tr>
</table>
</div>

<!--  tooltips-->

<div id="bib" class="balloonstyle">

	<p>Chameleon will look for journal holdings in any attached holding records by default.  If you
	track holdings in the marc bibliographic record, however, indicate the specific fields here.
	The plugin will look in BOTH the bib and attached holding records.</p>
	
	<p>Enter each field as a three digit marc tag number followed by an underscore, 
	followed by a single subfield code letter or number -- e.g., 866_a. 
	Separate multiple entries by a comma.</p>
</div>

<div id="runs" class="balloonstyle">

	<p>Holdings are typically expressed as a series of 'runs', usually broken-up by missing issues.
	For example: '1:1(1980)-10:12(1990), 14:1(1994)-'.  These runs are typically separated from
	each other using a delimiter, in the above example a comma, and a character to separate the 
	starting and ending points of the run, in the above example a dash.</p>

	<p>Sometimes you'll have more than one character delimitings runs, either to separate different 
	locations or as a hold over from a previous scheme.  You can use 'delimiter_replace' below to 
	tell chameleon to replace that older character with the new 'delimiter' character.</p>
	
	<ul>
	<li>delimiter: the character used to delimit each holding run, default is comma</li>
	<li>delimiter_replace: (optional) alternative delimiter that should be replaced with the delimiter above</li>
	<li>span: the character used to separate the start and end of a run, default is dash</li>
	</ul>

</div>

<div id="rolling" class="balloonstyle">


	<p>Some holdings statements include relative dates, or rolling holdings.  For example, 'Latest 2 years'
	or 'Last 6 months'.  These need to be expressed here.</p>

	<p>Separate multiple entries with a comma. Entries are case in-sensitive.</p>
	
	<p>Use the pound sign '#' as a wildcard for numbers.  Chameleon will assume that the pound sign 
	represents a number 1-3 digits in length.  In that way you can catch statements like 
	'Latest 2 years' and 'Latest 14 years' with the single entry 'Latest # years'.</p>
	
	<p>Keep in mind that chameleon matches down to the year level.  Statements like 'latest edition' 
	are meaningless to the plugin since it doesn't know how frequently a periodical is published.  
	Likewise, chameleon will look to round a statement about months up to a whole year.</p>

</div>

<div id="dates" class="balloonstyle">

	Some libraries will include the full dates with commas for frequently occuring titles, e.g., 'Jan 16, 1999'.  
	The comma messes-up the run delimiters.  So if you have these, set this value to 1 otherwise leave as 0.  Libraries 
	that have patterns like (1981-1990) should *not* set this to 1.

</div>

<div id="ignore" class="balloonstyle">

	This will cause Chameleon to ignore holdings at specific locations -- designed primarily for 
	when you have attached items records with 'Intenet' as a location, as with the Innovative ERM.
	Set here the name(s) of the location(s) you want to ignore. Use the publically displayed named 
	(e.g. 'Internet'), *not* an internal code.  Separate multiple entries with commas.
</div>

<div id="print-only" class="balloonstyle">

	This instructs Chameleon to show a link for the catalog if it find _any_ holdings, without processing
	those holdings first to see if the year matches.  Useful if you feel your holdings are too inconsistent
	to be parsed, but want Chameleon to _not_ show for matches on e-journals in the catalog.

</div>

<div id="avail" class="balloonstyle">

	<p>Chameleon will check whether the book is available for check-out as a condition for showing the 
	link to the catalog.</p>
	
	<p>Endeavor catalogs work without having to enter any info here. If you have an Innovative catalog, 
	however, you can set this value to indicate the status messages for available items.</p>
	
	<p>Separate multiple values by a comma. The statuses are the _public_ display name 
	(e.g., AVAILABLE, LIB USE ONLY), *not* the internal code.</p>

</div>

</form>
PAGE

print "<div style='margin: 20px; background-color: #FFFFCC; padding: 15px; clear: both; border: 1px solid #FF9900;'>";
print "<h3>Results</h3>";
print $cgi->pre($objChameleon->debug());
print "</div>";

print $cgi->end_html;


